package com.utils;

import com.mds.manager.utils.DateUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;

public class ResponseHelp {

	private ResponseHelp(){}
	
	private static JSONObject json;
	
	private static JSONArray arr;
	
	private static BackMsg backMsg;

	private static PubMsg pubMsg;

	private static OutMsg outMsg;

	/**
	 * 将普通对象变为字符串
	 * @param obj
	 * @return
	 */
	public static String responseText(Object obj){
		json = JSONObject.fromObject(obj);
		return json.toString();
	}

	/**
	 * 将集合变成字符串
	 * @param array
	 * @return
	 */
	public static String responseArrayToText(Object array){
		arr = JSONArray.fromObject(array);
		json = JSONObject.fromObject(arr);
		return json.toString();
	}

	/**
	 * 返回成功信息
	 * @param result
	 * @return
     */
	public static String responseApiSuccessText(Object result){
		backMsg = new BackMsg();
		pubMsg = new PubMsg();
		outMsg = new OutMsg();

		pubMsg.setRetCode(Constants.RESPON_CODE.SUCCESS);
		pubMsg.setRetMessage("请求成功！");
		pubMsg.setTimestamp(String.valueOf(new Date().getTime()));

		backMsg.setOut(result);
		backMsg.setPub(pubMsg);

		json = JSONObject.fromObject(backMsg);
		return json.toString();
	}

	/**
	 * 返回失败信息
	 * @param code
	 * @param message
     * @return
     */
	public static String responseApiErrorText(String code, String message){
		backMsg = new BackMsg();
		pubMsg = new PubMsg();
		outMsg = new OutMsg();

		pubMsg.setRetCode(code);
		pubMsg.setRetMessage(message);
		pubMsg.setTimestamp(String.valueOf(new Date().getTime()));

		backMsg.setPub(pubMsg);
		backMsg.setOut(null);

		json = JSONObject.fromObject(backMsg);
		return json.toString();
	}

}
